package com.cm.controller;


import com.cm.dto.CreateTemplateDto;
import com.cm.entity.Result;
import com.cm.server.TemplatesService;
import com.cm.utils.ResultUtil;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api(tags = "证照模板管理")
@RestController
@RequestMapping("/templates")
public class TemplatesController <T extends CreateTemplateDto>{

    @Autowired
    TemplatesService templatesService;

    @ApiOperation("创建证照模板")
    @RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Result create(@RequestBody @Validated T templateInfo) {
        return templatesService.createTemplate(templateInfo);
    }


    @ApiOperation("模板编辑")
    @RequestMapping(value = "/editor", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiImplicitParam(value = "模板ID", name = "tempID")
    public Result editor(@RequestBody @Validated CreateTemplateDto templateInfo, @RequestParam String tempID) {
        return templatesService.editor(templateInfo, tempID);
    }

    @ApiOperation("模板删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(value = "模板ID", name = "tempID"), @ApiImplicitParam(value = "邮箱验证码", name = "code")})
    public Result delete(@NotNull @Length(min = 10, max = 15) @RequestParam String tempID, @NotNull @RequestParam String code) {
        return templatesService.delect(tempID, code);
    }
}
